package com.aistar.serviceVO.impl;

import com.aistar.mapper.*;
import com.aistar.pojo.*;
import com.aistar.pojoVO.DynamicAllVO;
import com.aistar.serviceVO.DynamicAllVOService;
import com.aistar.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DynamicAllVOServiceImpl implements DynamicAllVOService {
    private final DynamicAllVOMapper dynamicAllVOMapper;

    @Autowired
    public DynamicAllVOServiceImpl(DynamicAllVOMapper dynamicAllVOMapper) {
        this.dynamicAllVOMapper = dynamicAllVOMapper;
    }


    @Override
    public ServerResponse getByDynamicId(Integer dynamicId) {
        DynamicAllVO dynamicAllVO = dynamicAllVOMapper.selectByPrimaryKey(dynamicId);
        if (dynamicAllVO != null)
            return ServerResponse.getSuccess(dynamicAllVO);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getByCustomerId(Integer customerId) {
        List<DynamicAllVO> dynamicAllVOList = dynamicAllVOMapper.selectByCustomerId(customerId);
        if (dynamicAllVOList != null)
            return ServerResponse.getSuccess(dynamicAllVOList);
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getByPageOrderByDynamicTime(Integer pageNum, Integer pageSize) {
        Page<Dynamic> page = PageHelper.startPage(pageNum, pageSize);
        List<DynamicAllVO> dynamicAllVOList = dynamicAllVOMapper.selectAll(
                DynamicAllVOMapper.DYNAMIC_TIME + "desc," + DynamicAllVOMapper.COMMENT_COUNT + "desc");
        if (dynamicAllVOList != null)
            return ServerResponse.getSuccess(page.toPageInfo());
        return ServerResponse.getFailed();
    }

    @Override
    public ServerResponse getByPageOrderByComment(Integer pageNum, Integer pageSize) {
        Page<Dynamic> page = PageHelper.startPage(pageNum, pageSize);
        List<DynamicAllVO> dynamicAllVOList = dynamicAllVOMapper.selectAll(
                DynamicAllVOMapper.COMMENT_COUNT + "desc," + DynamicAllVOMapper.DYNAMIC_TIME + "desc");
        if (dynamicAllVOList != null)
            return ServerResponse.getSuccess(page.toPageInfo());
        return ServerResponse.getFailed();
    }

}
